package net.uhuijia.software.softwareQcode.service.impl;

import net.uhuijia.appbase.service.CrudService;
import net.uhuijia.constants.DBConst;
import net.uhuijia.software.softwareQcode.persistence.dao.SoftwareQcodeDao;
import net.uhuijia.software.softwareQcode.persistence.model.SoftwareQcode;
import net.uhuijia.software.softwareQcode.persistence.model.SoftwareQcodeExample;
import net.uhuijia.software.softwareQcode.persistence.model.SoftwareQcodeExample.Criteria;
import net.uhuijia.software.softwareQcode.service.SoftwareQcodeService;
import net.uhuijia.software.softwarerequest.persistence.model.SoftwareRequestExample;
import net.uhuijia.user.usermall.persistence.model.UserMall;
import net.uhuijia.utils.IticConst;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

import static net.uhuijia.base.BaseCode.FAIL;

@Service
@Transactional
public class SoftwareQcodeServiceImpl extends CrudService<SoftwareQcodeDao,SoftwareQcode,SoftwareQcodeExample>
        implements SoftwareQcodeService{
    /**
     * 添加数据
     * @param user
     * @param qrcodeSize
     * @return
     */
    @Override
    public String addData(String url,UserMall user, Integer qrcodeSize){
        String result = FAIL;
        SoftwareQcode softwareQcode = new SoftwareQcode();
        softwareQcode.setUserId(user.getId());
        softwareQcode.setNickName(user.getNickName());
        softwareQcode.setQcodeHeight(qrcodeSize);
        softwareQcode.setQcodeUrl(url);
        softwareQcode.setQcodeWidth(qrcodeSize);
        result = this.add(softwareQcode);
        return result;
    }

    @Override
    public List<SoftwareQcode> historylist(HttpServletRequest request, UserMall user){
        SoftwareQcodeExample example = new SoftwareQcodeExample();
        IticConst.handlePageQueryDefault(request,example);
        example.setOrderByClause("CREATE_TIME DESC");
        Criteria criteria = example.createCriteria();
        criteria.andIsEffectiveEqualTo(DBConst.TRUE);
        criteria.andUserIdEqualTo(user.getId());
        return mapper.selectByExample(example);
    }

    @Override
    public  Integer countUserHistoryRequest(UserMall user){
        Integer count = null;
        SoftwareQcodeExample example = new SoftwareQcodeExample();
        Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(user.getId());
        count = this.count(example);
        return count;
    }
}
